package com.sky.service.impl;

import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.exception.AllTheException;
import com.sky.mapper.EmployeeMapper;
import com.sky.result.PageResult;
import com.sky.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.time.LocalDateTime;
import java.util.List;

import static com.sky.constant.MessageConstant.*;
import static com.sky.constant.StatusConstant.DISABLE;

/**
 * @Author：XueShiHao
 * @Date：2025/6/3 星期二 10:45:50
 * @Email:747278007@qq.com
 * @Description:
 **/
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public Employee login(EmployeeLoginDTO employeeLoginDTO) {
        String username= employeeLoginDTO.getUsername();
        String password=employeeLoginDTO.getPassword();
        //getByUsername()方法写在Mapper层，实现方法在EmployeeMapper.xml
        Employee employee = employeeMapper.getByUsername(username);
        /**
         * 1.账户不存在
         * 2.密码错误(查询账户对的密码与前端所传输过来的是否一致）
         * 3.账号被禁用
         */
        if(employee==null){
            throw new AllTheException(ACCOUNT_NOT_FOUND);
        }
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(employee.getPassword())){
            throw new AllTheException(PASSWORD_ERROR);
        }
        if(employee.getStatus()== DISABLE){
            throw new AllTheException(ACCOUNT_LOCKED);
        }


        return employee;
    }

    @Override
    public void save(EmployeeDTO employeeDTO) {
Employee employee=new Employee();
/**
 * 拷贝，保存除了前端传输过来的数据，还要保存以下的信息
 * 密码为默认，同时同样采用MD5加密
 */
        BeanUtils.copyProperties(employeeDTO,employee);
        employee.setStatus(StatusConstant.ENABLE);
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));//默认初始密码，MD5加密
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long currentId= BaseContext.getCurrentId();
        employee.setCreateUser(currentId);
        employee.setUpdateUser(currentId);
        employeeMapper.insert(employee);

    }

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        /**
         * 使用Github的分页工具PageHelper
         */
       PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
       Page<Employee> page=employeeMapper.pageQuery(employeePageQueryDTO);
       long total = page.getTotal();
       List<Employee> records=page.getResult();

        return new PageResult(total,records);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        Employee employee=new Employee();
        employee.setId(id);
        employee.setStatus(status);
        employeeMapper.update(employee);

    }

    @Override
    public Employee getById(Long id) {
        Employee employee=employeeMapper.getById(id);
        return employee;
    }


}
